﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.Controls;

namespace ShopOnline
{
    public partial class MyAccount : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (View_Customers.GetCurrentUser() == null)
            {
                Response.Redirect("Register.aspx");
                return;
            }

            lblBalance.Text = string.Format("{0:C}", View_Customers.GetCurrentUser().TotalAmount - View_Customers.GetCurrentUser().PaidAmount);
            lblTotalAmount.Text = string.Format("{0:C}", View_Customers.GetCurrentUser().TotalAmount);
            lblPaidAmount.Text = string.Format("{0:C}", View_Customers.GetCurrentUser().PaidAmount);

            if (View_Customers.GetCurrentUser().Price == CustomerType.CustomerPrice.SalePrice)
            {
                liBal.Visible = false;
                divBal.Visible = false;
            }

            ((ProductMaster)this.Master).SetPageTitle("My Account");

            if (!IsPostBack)
            {
                BannerKey = ShopOnline.BLL.BannerKey.MyAccount;
                txtEmail.Text = View_Customers.GetCurrentUser().s_Email;
                txtAddress.Text = View_Customers.GetCurrentUser().s_Address;
                txtPhone.Text = View_Customers.GetCurrentUser().s_Phone;
                txtName.Text = View_Customers.GetCurrentUser().s_CustomerName;

                gridOrder.Rebind();
            }
        }

        protected void btnChangePassword_OnClick(object sender, EventArgs e)
        {
            try
            {
                Customers cust = new Customers();
                cust.LoadByPrimaryKey(View_Customers.GetCurrentUser().ID);
                cust.Password = POSSystem.BLL.Utilities.CreateMD5Hash(txtNewPassword.Text);
                cust.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                cust.Save();
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Your password has been updated successfully!', 8000);", 1), true);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("EditCustomerCtrl.ascx.cs", "lnkRegiester_OnClick", ex.Message + ex.StackTrace);
            }
        }

        protected void btnRegister_OnClick(object sender, EventArgs e)
        {
            try
            {
                if (txtEmail.Text != "" && Customers.GetCustomerByEmail(txtEmail.Text, View_Customers.GetCurrentUser().ID).RowCount > 0)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'This email has been used. Please provide another.', 8000);", 1), true);
                    return;
                }

                Customers cust = new Customers();
                cust.LoadByPrimaryKey(View_Customers.GetCurrentUser().ID);
                cust.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                cust.CustomerName = txtName.Text;
                cust.Address = txtAddress.Text;
                cust.Phone = txtPhone.Text;
                cust.Email = txtEmail.Text;

                cust.Save();

                lblWelcome.Visible = false;

                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Update successfully. Thank for your registration!', 8000);", 1), true);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("EditCustomerCtrl.ascx.cs", "lnkRegiester_OnClick", ex.Message + ex.StackTrace);
            }
        }

        protected void gridOrder_OnNeedDataSource(object sender, EventArgs e)
        {
            View_CustomerOrders orders = new View_CustomerOrders();
            int count = orders.Search(View_CustomerOrders.ColumnNames.StatusID + "!=" + OrderStatus.Temporary.ToString() + " and " + View_CustomerOrders.ColumnNames.CustomerID + " = " + View_Customers.GetCurrentUser().s_ID,
                View_CustomerOrders.ColumnNames.ID + " desc", dataPagerDown.CurrentPageIndex * dataPagerDown.PageSize, dataPagerDown.PageSize);
            gridOrder.DataSource = orders.DefaultView;
            dataPagerDown.TotalItems = count;
        }

        protected void gridOrderItems_OnNeedDataSource(object sender, EventArgs e)
        {
            int orderID = int.Parse(hidOrderID.Value);
            View_OrderItems items = View_OrderItems.GetOrderItems(orderID);
            gridOrderItems.DataSource = items.DefaultView;
        }

        protected void btnSelectOrder_OnClick(object sender, EventArgs e)
        {
            gridOrderItems.Rebind();
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "$('#tabs').tabs({selected:2});", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "selectOrder('" + hidOrderID.Value + "');", true);
        }

        public string GetDefaultPic()
        {
            return "~/images/namini.png";
        }

        protected void dataPager_OnPageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            gridOrder.Rebind();
        }
    }
}